package com.siddy.bbs.http;

import java.io.IOException;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Response;

public class VolleyPost<T> extends VolleyRequest<T> {

	private ResponseParser<T> mResponseParser;

	private ResponseListener<T> mResponseListener;

	private VolleyContent mContent;

	public VolleyPost(String url, VolleyContent content, ResponseParser<T> parser,
			ResponseListener<T> listener) {
		super(Method.POST, url, parser, listener);
		mContent = content;
	}

	public ResponseParser<T> getResponseParser() {
		return mResponseParser;
	}

	public void setResponseParser(ResponseParser<T> mResponseParser) {
		this.mResponseParser = mResponseParser;
	}

	public ResponseListener<T> getResponseListener() {
		return mResponseListener;
	}

	public void setResponseListener(ResponseListener<T> mResponseListener) {
		this.mResponseListener = mResponseListener;
	}

	public VolleyContent getContent() {
		return mContent;
	}

	public void setContent(VolleyContent mContent) {
		this.mContent = mContent;
	}

	public VolleyPost(String url, VolleyContent content) {
		this(url, content, null, null);
	}

	@Override
	public String getBodyContentType() {
		if (mContent != null) {
			return mContent.getType();
		}
		return super.getBodyContentType();
	}

	@Override
	public byte[] getBody() throws AuthFailureError {
		try {
			return mContent.getContent();
		} catch (IOException ie) {
			throw new AuthFailureError("build body failed", ie);
		}
	}

	@Override
	protected Response<T> parseNetworkResponse(NetworkResponse response) {
		return mResponseParser.parse(response);
	}

	@Override
	protected void deliverResponse(T response) {
		if (mResponseListener != null) {
			mResponseListener.onResponse(response);
		}
	}

}